import { Dispatch } from 'redux';
import {
    REQUEST_MOVIE_INFO,
    REQUEST_MOVIE_UPLOAD,

} from '../constants';

import {
    movieInfo,
    movieUpload,
} from '@api/movie';

import { IMovieResInfo, IMovieUploadParam, IMovieUploadResInfo, IMovieGetParam } from '@typings/movie';

/** 请求电影信息接口 */
export interface IRequestMovieInfoAction {
    type: REQUEST_MOVIE_INFO;
    result: IMovieResInfo | null;
}

/** 请求电影信息接口 */
export const requestMovieInfo = (
    params: IMovieGetParam,
    callback?: (data:any) => void
) => async (dispatch: Dispatch<IRequestMovieInfoAction>) => {
    const { data } = await movieInfo(params);

    if (data.isOk) {
        // dispatch({
        //     type: REQUEST_MOVIE_INFO,
        //     result: data.result
        // });
        if (typeof callback === 'function') {
            callback(data.result);
        }
    }
};
/** 上传电影信息接口 */
export interface IUploadMovieInfoAction {
    type: REQUEST_MOVIE_UPLOAD;
    result: IMovieUploadResInfo | null;
}

/** 上传电影信息接口 */
export const requestMovieUpload = (
    params: IMovieUploadParam,
    callback?: () => void
) => async (dispatch: Dispatch<IUploadMovieInfoAction>) => {
    const { data } = await movieUpload(params);

    if (data.isOk) {
        dispatch({
            type: REQUEST_MOVIE_UPLOAD,
            result: data.result
        });
        if (typeof callback === 'function') {
            callback();
        }
    }
};

/** 总类型 */
export type IMovieAction =
    | IRequestMovieInfoAction
    | IUploadMovieInfoAction;